package com.gsafety.libraryktx.expand

import android.view.View
import android.view.ViewTreeObserver
import com.gsafety.library.Exts.Ext
import com.gsafety.library.common.OnUnshakClickListener

// 控件是否可见
inline fun <reified T:View> T.isVisible() = Ext.isVisible(this)

inline fun <reified T : View> T.hide() = Ext.hide(this)
inline fun <reified T : View> T.show() = Ext.show(this)
inline fun <reified T : View> T.gone() = Ext.gone(this)
/**
 * 切换VISIBLE显示Viw
 */
inline fun <reified T : View> T.toggleHide() = Ext.toggleHide(this)

/**
 * 测量自定义View的高度
 */
inline fun <reified T : View> T.measureHeight(heightMeasureSpec: Int) =
        Ext.measureHeight(heightMeasureSpec)

/**
 * 测量自定义View的宽度
 */
inline fun <reified T : View> T.measureWidth(widthMeasureSpec: Int) =
        Ext.measureWidth(widthMeasureSpec)

/**
 * 弹出软键盘
 */
inline fun <reified T : View> T.showKeyboard() = Ext.showKeyboard(context, this)

/**
 * 切换GONE显示Viw
 */
inline fun <reified T : View> T.toggleGone() = Ext.toggleGone(this)





/**
 * 当控件测量完成
 */
inline fun <T : View> T.afterMeasured(crossinline f: T.(width: Int, height: Int) -> Unit)
{
    viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener
    {
        override fun onGlobalLayout()
        {
            if (measuredWidth > 0 && measuredHeight > 0)
            {
                viewTreeObserver.removeOnGlobalLayoutListener(this)
                f(measuredWidth, measuredHeight)
            }
        }
    })
}

/**
 * 防止抖动点击事件
 * 500毫秒之内不允许两次点击
 */
inline fun <reified T : View> T.setUnShakeClickListener(crossinline block: (view: T) -> Unit)
{
    Ext.setUnShakeClickListener(this, object : OnUnshakClickListener()
    {
        override fun onClick(view: View, lastClickTime: Long)
        {
            if (view is T)
            {
                block.invoke(view)
            }
        }
    })
}